package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.form.SearchForm;
import com.atguigu.guli.service.edu.feign.OssFileFeignClient;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2020-11-10
 */


/*
*使用swagger:
* 1.controller
* @Api 描述controller
* @ApiOperation 描述controller的一个方法
* @ApiParam 描述形参
*
* 2.bean
* @ApiModel 描述javabean
* @ApiModelProperty 描述javabean的属性
*
*
* */
@CrossOrigin
@Api(tags = "讲师管理模块")
@RestController
//@Controller
@RequestMapping("/admin/edu/teacher")
@Slf4j//lombok 提供logback日志类对象的注解
public class AdminTeacherController {

    @Autowired
    TeacherService teacherService;

    @Autowired
    OssFileFeignClient ossFileFeignClient;


//    根据关键字查询
    @ApiOperation("根据关键字查询讲师列表")
    @GetMapping("/list/name/{key}")
    public R selectNameListByKey(
            @ApiParam(value = "查询关键字",required = true)
            @PathVariable String key){
        List<Map<String,Object>> nameList= teacherService.selectNameListByKey(key);
        System.out.println("===============关键字查询===AdminTeacherController======");
        return R.ok().data("nameList",nameList);

    }

    //    批量删除
    @ApiOperation("批量删除讲师")
    @DeleteMapping("/batchDelete")
    public R batchDelete(@RequestBody List<String> ids) {//1.请求参数: ids=1,2,3 请求体:[1,2,3]
//        {"key":value} json对象  ,[1,2,"xx",{"k":v}] json数组
         teacherService.removeByIds(ids);
         return R.ok().message("批量删除成功");

    }
//    根据id查询讲师
    @ApiOperation("查询指定讲师")
    @GetMapping("/get/{id}")
    public R getById(@PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("item",teacher);
    }

//    查询所有讲师
    @ApiOperation("查询所有讲师")
    @GetMapping("/list")
//    @ResponseBody
    public R list(){
        List<Teacher> list = teacherService.list();
        return R.ok().data("items",list).message("获取讲师列表成功");
    }
//    新增
    @ApiOperation("新增讲师")
    @PostMapping("/save")
    public R save(@RequestBody Teacher teacher){
//        System.out.println("teacher = " + teacher);
        log.info("接受到的teacher对象:{},{}",teacher,123);
        boolean b = teacherService.save(teacher);
        if (b){
            return R.ok().message("新增成功");
        }
        return R.error().message("新增失败");

    }
//    更新
    @ApiOperation("更新讲师")
    @PutMapping("/update")
    public R update(@RequestBody Teacher teacher){
//        System.out.println("teacher = " + teacher);
        log.info("更新teacher对象"+teacher);
        boolean b = teacherService.updateById(teacher);
        if (b){
            return R.ok().message("更新成功");
        }
        return R.error().message("更新失败");

    }

    /*
    get方式：
        请求报文没有请求体
        提交参数时，参数在url地址后拼接
        url?k=v&k2=v2

     post方式：
           请求报文由请求体
           post的请求参数通过请求体提交[参数在请求体中如果是json字符串]
           后端controller方法需要使用 @RequestBody接受入参
     */

//带条件的分页查询
    @ApiOperation("带条件查询讲师分页数据")
    @GetMapping("/list/{pageNum}/{size}")
    public R listPage(@ApiParam("查询条件对象")SearchForm searchForm,
                      @ApiParam("页码") @PathVariable Long pageNum,
                      @ApiParam("每页记录条数") @PathVariable Long size){

        Page<Teacher> page =teacherService.pageByCondition(searchForm,pageNum,size);
        return R.ok().data("page",page);
    }




    //删除
    @ApiOperation(value = "根据ID删除讲师",notes = "根据ID删除讲师")
    @DeleteMapping("/remove/{id}")
    public R removeById(@ApiParam(required = true,value = "讲师ID") @PathVariable("id") String id){
//        再删除前获取讲师信息,为了后面删除时根据讲师信息获取path路径
        Teacher teacher = teacherService.getById(id);
        boolean result = teacherService.removeById(id);
        if (result){
            //删除讲师oss中保存的头像
            //服务之间的远程访问
            /**
             * 1、RPC
             *     远程过程调用  Dubbo使用
             *
             * 2、RestFul： HTTP+JSON
             *      Ribbon(对HttpClient的封装)、OpenFeign(对ribbon的封装)
             *
             */
            if (!StringUtils.isEmpty(teacher.getAvatar())){
               ossFileFeignClient.deleteFile("avatar", teacher.getAvatar());
            }

            return R.ok().message("删除成功");
        }else {
            return R.error().message("数据不存在");
        }

    }
}

